package com.qianfeng.xinyihezi.xinyihezi.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.MyItemsRecyclerViewAdapter;
import com.qianfeng.xinyihezi.xinyihezi.config.MyNetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.MyRetrofit;
import com.qianfeng.xinyihezi.xinyihezi.model.AllBean;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 作者：王加威 on 2016/11/13 18:20
 */

public class ItemsFragment extends Fragment {

    private String mTitle;
    private RecyclerView mRecyclerView;
    private Retrofit mRetrofit;
    private ProgressDialog mDialog;
    private List<AllBean.DataBean.ActivityBean> mAllBeen;
    private MyItemsRecyclerViewAdapter mAdapter;
    private View mView;
    private LinearLayoutManager mMLayoutManager;
    private int page=1;
    private String[] titles={"全部","吃货最爱","美妆美艳","居家生活","配饰箱包","数码周边","卡通动漫","快速选礼"};
    private TabLayout mTabLayout;
    private String mString;
    private FloatingActionButton mButton;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mView = LayoutInflater.from(getActivity()).inflate(R.layout.items_fragment,null);
        initView(mView);
        loadData(0,1);
        setAdapter();
        setListener();
        return mView;
    }

    private void setAdapter() {
        mMLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mMLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }


    private void setListener() {

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mString = null;
                String title =tab.getText().toString();
                mString=title;
                mAllBeen.clear();
                if (mString.equals("全部")) {
                    loadData(0,1);
                    setScrollListener(mString);
                }else if (mString.equals("吃货最爱")){
                    loadData(3,1);
                }else if (mString.equals("美妆美艳")){
                    loadData(4,1);
                }else if (mString.equals("居家生活")){
                    loadData(1,1);
                }else if (mString.equals("配饰箱包")){
                    loadData(8,1);
                }else if (mString.equals("数码周边")){
                    loadData(5,1);
                }else if (mString.equals("卡通动漫")){
                    loadData(6,1);
                }else {
                    loadData(7,1);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void loadData(int page1,int page2) {
        mDialog.show();
        mRetrofit = new Retrofit.Builder().baseUrl(MyNetConfig.BASE_PATH).build();
        MyRetrofit retrofit=mRetrofit.create(MyRetrofit.class);
        retrofit.getAllData(page1,page2).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        mDialog.dismiss();
                        AllBean allBean =new Gson().fromJson(response.body().string(),AllBean.class);
                        mAllBeen.addAll(allBean.data.activity);
                        mAdapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.smoothScrollToPosition(0);
            }
        });
    }

    private void initView(View view) {
        mButton= (FloatingActionButton) view.findViewById(R.id.btn);
        mTabLayout= (TabLayout) view.findViewById(R.id.main_TabLayout);
        mAllBeen =new ArrayList<>();
        mAdapter=new MyItemsRecyclerViewAdapter(getActivity(),mAllBeen);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.items_fragment_gridview);
        mDialog=new ProgressDialog(getActivity());
        mDialog.setMessage("正在加载");
        for (int i = 0; i < titles.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }
    }
        private void setScrollListener(String string){
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    if (dy>3 && mButton.getVisibility()== View.GONE) {
                        mButton.setVisibility(View.VISIBLE);
                    }else if(dy<-3 && mButton.getVisibility()==View.VISIBLE){
                        mButton.setVisibility(View.GONE);
                    }
                    int visible =mMLayoutManager.getChildCount();
                    int total=mMLayoutManager.getItemCount();
                    int past=mMLayoutManager.findFirstCompletelyVisibleItemPosition();
                    if ((visible+past)>total) {
                        if (mString.equals("全部")) {
                            page++;
                            loadData(0,page);
                        }
                    }

                }
            });
        }
}
